/**
 * 动物诊疗机构日常巡查记录
 * @constructor
 */
function AnimalDaily() {
}

AnimalDaily.init = function () {
    getName1();
    if($("#k").val()==2){
        document.getElementById("toolbar").style.display = "none";
    }
    $('#tableList').bootstrapTable({
        url: '/animalDaily/tableSearch',
        method: 'post',
        striped: true, //是否显示行间隔色
        toolbar: "#toolbar",
        sidePagination: "true",
        dataField: "data",//这是返回的json数组的key.默认是"rows".这里只有前后端约定好就行
        showToggle: true, //是否显示详细视图和列表视图的切换按钮
        striped: true, // 是否显示行间隔色
//search : "true",
        uniqueId: "id",
        pageList: [5, 10, 20, 50, 100],//可选择单页记录数
        pageSize: "10",
        pagination: true, // 是否分页
        sortable: true, // 是否启用排序
        sidePagination: 'server', // 设置为服务器端分页     客户端：client
        search: false,
        showColumns: true,
        showRefresh: true,
        showExport: true,
//得到查询的参数
        queryParams: function (params) {
//这里的键的名字和控制器的变量名必须一直，这边改动，控制器也需要改成一样的
            var query_params = {
//********table的参数 不变
                pageSize: params.limit,                         //页面大小
                pageIndex: (params.offset / params.limit) + 1,   //页码
                sort: params.sort,      //排序列名
                sortOrder: params.order, //排位命令（desc，asc）
//********
//获取查询的对象，往下加入
                timeInterval: $('#search_InspectionDate').val(), // 请求时向服务端传递的参数
            };
            return query_params;
        },
        columns: [
            {
//field: 'Number',//可不加
                title: '序号',//标题  可不加
                align: "center",
                width: 40,
                formatter: function (value, row, index) {
                    var pageSize = $('#tableList').bootstrapTable('getOptions').pageSize;//通过表的#id 可以得到每页多少条
                    var pageNumber = $('#tableList').bootstrapTable('getOptions').pageNumber;//通过表的#id 可以得到当前第几页
                    return pageSize * (pageNumber - 1) + index + 1;//返回每条的序号： 每页条数 * （当前页 - 1 ）+ 序号
                }
            },
            {field: 'inspectionDate', title: '巡查日期', align: 'center'},
            {field: 'inspectionName', title: '被巡查对象名称', align: 'center'},
            {field: 'legalPerson', title: '法人或负责人', align: 'center'},
            {field: 'address', title: '从业地址', align: 'center'},
            {field: 'phone', title: '联系电话', align: 'center'},
            {field: 'treatment', title: '诊疗范围', align: 'center'},
            {field: 'licence', title: '是否办理动物诊疗许可证', align: 'center'},
            {field: 'changeSituation', title: '取得《动物诊疗许可证》后条件是否变化', align: 'center'},
            {field: 'qualification', title: '显著位置是否公示许可证和从业人员资格证', align: 'center'},
            {field: 'medicalRecord', title: '是否规范使用病历', align: 'center'},
            {field: 'prescription', title: '是否规范使用处方笺', align: 'center'},
            {field: 'archives', title: '病历档案是否保存三年以上', align: 'center'},
            {field: 'apparatus', title: '是否规范使用兽药和兽医器械', align: 'center'},
            {field: 'activity', title: '是否按规定年度报告活动', align: 'center'},
            {field: 'employees', title: '从业人员是否取得执业或助理执业兽医资格证', align: 'center'},
            {field: 'registration', title: '是否经兽医执业注册、登记备案', align: 'center'},
            {field: 'keepRecord', title: '变更诊疗机构有无重新办理注册或者备案', align: 'center'},
            {field: 'issuePrescription', title: '是否实行注册执业兽医师出具处方', align: 'center'},
            {field: 'standardFormat', title: '是否使用规范格式的处方', align: 'center'},
            {field: 'rationalUse', title: '是否按规定合理用药', align: 'center'},
            {field: 'fakeMedicine', title: '有无使用假劣兽药和国家禁用的药品、化合物', align: 'center'},
            {field: 'periodicReport', title: '是否按规定定期报告执业活动情况', align: 'center'},
            {field: 'lend', title: '是否有出证、出租、出借执业证书情况', align: 'center'},
            {field: 'supervision', title: '监督检查情况', align: 'center'},
            {
                field: 'inspectionPhoto', title: '巡查现场图片',align: 'center',width: 200,
                formatter: function (value) {
                    return "<a href='"+value+"' target='_blank'>查看图片</a>";
                    // return "<img src=\"" + value + "\" class=\"img-responsive\">";
                }
            },
            {field: 'inspectionCompany', title: '巡查单位名称	', align: 'center'},
            {field: 'remark', title: '备注', align: 'center'},
            {field: 'inspectionPerson', title: '巡查人员签字', align: 'center'},
            {field: 'toInspectionPerson', title: '被巡查人员签字', align: 'center'},
            {
                title: '操作',
                align: 'center',
                formatter: function (value, row, index) {
                    var optionHtml = "";
                        optionHtml = optionHtml + "<a class=\"btn btn-success btn-xs\"  href=\"javascript:void(0)\"  onclick='actionRecord(" + JSON.stringify(row) + ",\"edit\")' title=\"修改\" ><i class=\"fa fa-edit\"></i>修改</a>";
                        optionHtml = optionHtml + " <a class=\"btn btn-danger btn-xs\" href=\"javascript:void(0)\"  onclick='actionRecord(" + JSON.stringify(row) + ",\"delete\")'  title=\"删除\"><i class=\"fa fa-remove\"></i>删除</a>"
                    return optionHtml;

                }
            }
        ]
    });
}

laydate.render({
    elem: '#text_inspectionDate',
    theme: 'molv',
    type:"date",
});
laydate.render({
    elem: '#search_InspectionDate',
    range: true,
});
//查询方法****************************
function doSearch() {
// $('#tableList').bootstrapTable(('refresh')); // 很重要的一步，刷新url！
    $("#tableList").bootstrapTable("refreshOptions", {pageNumber: 1});
}


//重置****************************
function doRest() {
    $("#search_InspectionDate").val("");
    // $('#tableList').bootstrapTable(('refresh')); // 很重要的一步，刷新url！
    $("#tableList").bootstrapTable("refreshOptions", {pageNumber: 1});
}

//增加方法****************************
var Action = "";
var id = 0;
var pxt = "/animalDaily"; //控制器名称
function addRecord() {
    id = 0;
    Action = "add";
    $("#myform")[0].reset();

}

//增加/修改数据保存****************************
function saveRecord() {
    if (!validateForm()) return false;
//创建传递的参数
    var postdata = {
        id: id,
        action: Action,

        inspectionDate: $("#text_inspectionDate").val(),
        inspectionName: $("#text_inspectionName").find("option:selected").text(),
        inspectionId: $("#text_inspectionName").val(),
        legalPerson: $("#text_legalPerson").val(),
        address: $("#text_address").val(),
        phone: $("#text_phone").val(),
        treatment: $("#text_treatment").val(),
        licence: $("#text_licence").val(),
        changeSituation: $("#text_change").val(),
        qualification: $("#text_qualification").val(),
        medicalRecord: $("#text_medicalRecord").val(),
        prescription: $("#text_prescription").val(),
        archives: $("#text_archives").val(),
        apparatus: $("#text_apparatus").val(),
        activity: $("#text_activity").val(),
        employees: $("#text_employees").val(),
        registration: $("#text_registration").val(),
        keepRecord: $("#text_keepRecord").val(),
        issuePrescription: $("#text_issuePrescription").val(),
        standardFormat: $("#text_standardFormat").val(),
        rationalUse: $("#text_rationalUse").val(),
        fakeMedicine: $("#text_fakeMedicine").val(),
        periodicReport: $("#text_periodicReport").val(),
        lend: $("#text_lend").val(),
        supervision: $("#text_supervision").val(),
        inspectionPhoto:InspectionPhoto,
        inspectionPerson: $("#text_inspectionPerson").val(),
        toInspectionPerson: $("#text_toInspectionPerson").val(),
        inspectionCompany: $("#text_inspectionCompany").val(),
        remark: $("#text_remark").val(),
    };
// console.log(JSON.stringify(postdata));
//发送异步请求到后台保存用户数据
    $.ajax({
        url: pxt + (Action == "add" ? "/save" : "/update"),
        type: "post",
        data: JSON.stringify(postdata),
        contentType: "application/json",
        dataType: "json",
        success: function (data) {
            if (data.code === "200") {
                $('#myModalAdd').modal('hide')
                doSearch();
                swal("操作成功!", "请继续操作", "success");
            } else {
                swal("操作失败!", "请重试", "error");
            }
        }
    });
}

//操作相关方法****************************
function actionRecord(data, type) {
    httpInspectionPhoto=data.inspectionPhoto;
    if (type == "edit") {
//修改
        id = data.id;
        Action = "edit";
        $("#myform")[0].reset();
        $("#save").removeAttr("disabled");
        WriteData(data)
        $('#myModalAdd').modal('show')
    } else {
//删除
        swal({
            title: "确认是否删除",
            text: "是否确认要删除该条记录？",
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#DD6B55",
            confirmButtonText: "删除",
            cancelButtonText: "取消",
            closeOnConfirm: false
        }, function () {
//点击删除之后此处进行传递后台
            $.ajax({
                type: "delete",
                url: pxt + "/deleteById/" + data.id,
                dataType: "json",
                contentType: "application/json",
                success: function (data, status) {
                    if (data.code == "200") {
                        doSearch();
                        swal("删除成功!", "请继续操作", "success");
                    } else {
                        swal("删除失败!", "请重试", "error");
                    }
                }
            });

        });
    }
}

//给控件赋值****************************
function WriteData(data) {
    $("#text_inspectionDate").val(data.inspectionDate);
    $("#text_inspectionName").val(data.inspectionId);
    $("#text_legalPerson").val(data.legalPerson);
    $("#text_address").val(data.address);
    $("#text_phone").val(data.phone);
    $("#text_treatment").val(data.treatment);
    $("#text_licence").val(data.licence);
    $("#text_change").val(data.changeSituation);
    $("#text_qualification").val(data.qualification);
    $("#text_medicalRecord").val(data.medicalRecord);
    $("#text_prescription").val(data.prescription);
    $("#text_archives").val(data.archives);
    $("#text_apparatus").val(data.apparatus);
    $("#text_activity").val(data.activity);
    $("#text_employees").val(data.employees);
    $("#text_registration").val(data.registration);
    $("#text_keepRecord").val(data.keepRecord);
    $("#text_issuePrescription").val(data.issuePrescription);
    $("#text_standardFormat").val(data.standardFormat);
    $("#text_rationalUse").val(data.rationalUse);
    $("#text_fakeMedicine").val(data.fakeMedicine);
    $("#text_periodicReport").val(data.periodicReport);
    $("#text_lend").val(data.lend);
    $("#text_supervision").val(data.supervision);
    var strs=data.inspectionPhoto.split("/");
    var str=strs[strs.length-1];
    InspectionPhoto=str;
    document.getElementById("showImage").style.display = "block";
    $("#showImage").attr("src",httpInspectionPhoto);
    $("#text_inspectionPerson").val(data.inspectionPerson);
    $("#text_toInspectionPerson").val(data.toInspectionPerson);
    $("#text_inspectionCompany").val(data.inspectionCompany);
    $("#text_remark").val(data.remark);
}
var InspectionPhoto="";//图片路径
var httpInspectionPhoto=""//完整图片路径
//上传****************************
function ImportShipmentStatusList() {
    console.log("进入图片上传方法")
    var upname = "text_inspectionPhoto";
    if ($("#" + upname).val() == "") {
        alert("请选择上传图片");
        return;
    }
    $.ajaxFileUpload({
        url: pxt+'/uploadImages',
        secureuri: false,
        fileElementId: upname,
        dataType: 'json',
        success: function (data, status) {
            InspectionPhoto = data.url;
            httpInspectionPhoto=data.httpUrl;
            getImage(data.httpUrl);
            //隐藏文件选择
            $("#uploadImages").css("display","none");
            //图片回显
            $("#showImage").css("display","");
            $("#showImage").attr("src",data.httpUrl);
        },
        error: function (data, status, e) {
            console.log("上传图片失败",e);
        }
    });
}
function getImage(url) {
    var html="";
    // html=html+"<div class=\"row\">";
    html=html+"<div class=\"col-lg-9\">";
    html=html+"<div class=\"form-group\" style='width:420px; height:250px'>";
    html=html+"<img style='width:420px; height:250px' src=\""+url+"\" />";
    html=html+"<span class=\"label label-danger pull-right\" style=\"cursor:pointer\" onclick=\"delimage();\">删除</span>";
    html=html+"</div>";
    html=html+"</div>";
    html=html+"</div>";
}
function delimage() {
    //清空file
    var test = document.getElementById('text_inspectionPhoto');
    test.value = '';
}
//表单验证*****************************
function validateForm() {
//validate方法参数可选
    return $("#myform").validate({
        rules: {

            text_inspectionDate: {
                required: true,//必填
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            },
            text_inspectionName: {
                required: true,//必填
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            },
            text_legalPerson: {
                required: true,//必填
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            },
            text_address: {
                required: true,//必填
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            },
            text_phone: {
                required: true,//必填
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            },
            text_treatment: {
                required: true,//必填
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            },
            text_licence: {
                required: true,//必填
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            },
            text_change: {
                required: true,//必填
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            },
            text_qualification: {
                required: true,//必填
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            },
            text_medicalRecord: {
                required: true,//必填
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            },
            text_prescription: {
                required: true,//必填
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            },
            text_archives: {
                required: true,//必填
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            },
            text_apparatus: {
                required: true,//必填
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            },
            text_activity: {
                required: true,//必填
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            },
            text_employees: {
                required: true,//必填
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            },
            text_registration: {
                required: true,//必填
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            },
            text_keepRecord: {
                required: true,//必填
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            },
            text_issuePrescription: {
                required: true,//必填
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            },
            text_standardFormat: {
                required: true,//必填
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            },
            text_rationalUse: {
                required: true,//必填
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            },
            text_fakeMedicine: {
                required: true,//必填
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            },
            text_periodicReport: {
                required: true,//必填
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            },
            text_lend: {
                required: true,//必填
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            },
            text_supervision: {
                required: true,//必填
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            },
            text_inspectionPerson: {
                required: true,//必填
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            },
            text_toInspectionPerson: {
                required: true,//必填
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            },
            text_inspectionCompany: {
                required: true,//必填
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            },
        },
//错误信息提示
        messages: {
            text_inspectionDate: {
                required: "(必填)",
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            },
            text_inspectionName: {
                required: "(必填)",
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            },
            text_legalPerson: {
                required: "(必填)",
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            },
            text_address: {
                required: "(必填)",
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            },
            text_phone: {
                required: "(必填)",
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            },
            text_treatment: {
                required: "(必填)",
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            },
            text_licence: {
                required: "(必填)",
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            },
            text_change: {
                required: "(必填)",
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            },
            text_qualification: {
                required: "(必填)",
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            },
            text_medicalRecord: {
                required: "(必填)",
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            },
            text_prescription: {
                required: "(必填)",
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            },
            text_archives: {
                required: "(必填)",
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            },
            text_apparatus: {
                required: "(必填)",
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            },
            text_activity: {
                required: "(必填)",
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            },
            text_employees: {
                required: "(必填)",
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            },
            text_registration: {
                required: "(必填)",
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            },
            text_keepRecord: {
                required: "(必填)",
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            },
            text_issuePrescription: {
                required: "(必填)",
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            },
            text_standardFormat: {
                required: "(必填)",
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            },
            text_rationalUse: {
                required: "(必填)",
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            },
            text_fakeMedicine: {
                required: "(必填)",
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            },
            text_periodicReport: {
                required: "(必填)",
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            },
            text_lend: {
                required: "(必填)",
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            },
            text_supervision: {
                required: "(必填)",
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            },
            text_inspectionPerson: {
                required: "(必填)",
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            },
            text_toInspectionPerson: {
                required: "(必填)",
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            },
            text_inspectionCompany: {
                required: "(必填)",
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            },
        },
    }).form();

}
//给下拉选赋值
function getName1() {
    var postdata={
        types:1,
    };
    $.ajax({
        url: "/mechanism/getName",
        type:"post",
        data: JSON.stringify(postdata),
        contentType:"application/json",
        dataType:"json",
        success:function(data){
            $('#text_inspectionName').empty();
            $('#text_inspectionName').append("<option value='-1'>请选择被巡查对象名称</option>");
            $.each(data.data,function (index,item) {
                $('#text_inspectionName').append("<option value='"+item.id+"'>"+item.name+"</option>");
            })
        }
    });
}
//根据下拉选自动生成表格内容
function getName2(a) {
    console.log("进入")
    var postdata={
        types:1,
        id:a,
    };
    $.ajax({
        url: "/mechanism/getName",
        type:"post",
        data: JSON.stringify(postdata),
        contentType:"application/json",
        dataType:"json",
        success:function(data){
            if (data.data.length <= 0){
                $('#text_legalPerson').val("");
                $('#text_address').val("");
                $('#text_phone').val("");
                return false;
            }
            $.each(data.data,function (index,item) {
                $('#text_legalPerson').val(item.userName);
                $('#text_address').val(item.fullAddress);
                $('#text_phone').val(item.phone);
            })
        }
    });
}